package dingding.流操作.StreamDemo;

import org.junit.Test;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author liudingding
 * @ClassName Test5
 * @description
 * @date 2020/6/9 7:19 下午
 */
public class Test5 {

    /**
     * 将int类型的数组转化为string类型的数组
     *
     * 在java8的api中是没有直接转化的api的
     * 我们可以先将数组转化为list，然后list在转化为数组
     */
    @Test
    public void test1(){
        int[] array = {3,32,321};
        Arrays.stream(array)
                .mapToObj(String::valueOf)
                .collect(Collectors.toList())
                .toArray(new String[]{});
    }

    /**
     * 将int数组转化为String类型，并将每个字符拼接在一起
     */
    @Test
    public void test2(){
        int[] array = {3,32,321};
        Stream<Object> a = Arrays.stream(array)
                .mapToObj(String::valueOf);
        System.out.println(a.getClass());
    }


    /**
     * 找出数组或集合中最大最小的值
     */
    @Test
    public void test3(){
        int[] array = {1,3,53,23};
        List<Integer> list = Arrays.asList(1,32,445,43);
        int aMax = Arrays.stream(array).max().getAsInt();
        int aMin = Arrays.stream(array).min().getAsInt();
        int lMax = Collections.max(list);
        int lMin = list.stream().min(Comparator.naturalOrder()).orElse(null);
    }


    @Test
    public void test4(){
        Map<String, Long> map1 = new HashMap<>();

        map1.put("Apple", 2L);
        map1.put("Banana", 2L);

        Map<String, Long> map2 = new HashMap<>();

        map2.put("Apple", 2L);
        map2.put("Orange", 2L);

        Map<String, Long> map3 = new HashMap<>();

        map3.put("Banana", 2L);
        map3.put("Guava", 2L);

        List<Map<String, Long>> mapList = new ArrayList<>();

        mapList.add(map1);
        mapList.add(map2);
        mapList.add(map3);

        final Optional<Map<String, Long>> reduce = mapList.stream()
                .reduce((firstMap, secondMap) -> Stream.concat(firstMap.entrySet().stream(), secondMap.entrySet().stream())
                .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue,
                        (countInFirstMap, countInSecondMap) -> countInFirstMap + countInSecondMap)));

        System.out.println(reduce.get());

        ArrayList<Map> resultList = new ArrayList<Map>();
        reduce.ifPresent(resultList::add);
        System.out.println("result:"+resultList);
    }
}
